﻿using System;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x02000331 RID: 817
	internal class ToListObservable<TSource> : OperatorObservableBase<IList<TSource>>
	{
		// Token: 0x060010F3 RID: 4339 RVA: 0x0004E178 File Offset: 0x0004C578
		public ToListObservable(IObservable<TSource> source) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
		}

		// Token: 0x060010F4 RID: 4340 RVA: 0x0004E18D File Offset: 0x0004C58D
		protected override IDisposable SubscribeCore(IObserver<IList<TSource>> observer, IDisposable cancel)
		{
			return this.source.Subscribe(new ToListObservable<TSource>.ToList(observer, cancel));
		}

		// Token: 0x040009FC RID: 2556
		private readonly IObservable<TSource> source;

		// Token: 0x02000332 RID: 818
		private class ToList : OperatorObserverBase<TSource, IList<TSource>>
		{
			// Token: 0x060010F5 RID: 4341 RVA: 0x0004E1A1 File Offset: 0x0004C5A1
			public ToList(IObserver<IList<TSource>> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x060010F6 RID: 4342 RVA: 0x0004E1B8 File Offset: 0x0004C5B8
			public override void OnNext(TSource value)
			{
				try
				{
					this.list.Add(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x060010F7 RID: 4343 RVA: 0x0004E214 File Offset: 0x0004C614
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060010F8 RID: 4344 RVA: 0x0004E24C File Offset: 0x0004C64C
			public override void OnCompleted()
			{
				this.observer.OnNext(this.list);
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040009FD RID: 2557
			private readonly List<TSource> list = new List<TSource>();
		}
	}
}
